<#include "../common/header.htm">
<div class="content-wrapper">
	<section class="content-header">
		<ol class="breadcrumb">
			<li><a href="${ctx}/">首页</a></li>
			<li class="active"><a href="${ctx}/sensitivewords/list">敏感词管理</a></li>
		</ol>
	</section>
	<section class="content">
		<form action="${ctx}/sensitivewords/list" method="get" id="listForm">
			<div class="box">
				<div class="box-header">
					<div class="row RowSearchWork">
						<div class="col-sm-12">
							<div class="col-sm-2">
								<label>敏感词：</label>
								<input type="text" class="form-control" name="sensitiveWord" value="${paramsSensitiveWords.sensitiveWord!}" placeholder="请输入敏感词" />
							</div>
							<div class="col-sm-2">
								<label>操作人：</label>
								<input type="text" class="form-control" name="createUserName" value="${paramsSensitiveWords.createUserName!}" placeholder="请输入操作人" />
							</div>
							<div class="col-sm-3" style="display: -webkit-box;margin-right: 66px;">
								<label>操作时间：</label>
				                <div class="input-group">
				                 	<input type="text" class="form-control pull-right" name="beginCreateTimeStr" value="${paramsSensitiveWords.beginCreateTimeStr!}" id="starTime" autocomplete="off" placeholder="请输入开始时间" >
				                	<div class="input-group-addon">
				                    	<i>-</i>
				                  	</div>
				                 	<input type="text" class="form-control pull-right" name="endCreateTimeStr" value="${paramsSensitiveWords.endCreateTimeStr!}" id="endTime" autocomplete="off" placeholder="请输入结束时间" >
				                </div>
							</div>
							<div class="col-sm-4">
								<input type="button" class="btn btn-default btn-search" onclick="sensitiveWords.query();" value="查询" />
								<input type="button" style="margin-left: 10px;"  class="btn btn-primary" onclick="sensitiveWords.form()" value="新增" />
								<input type="button" style="margin-left: 10px;"  class="btn btn-primary" onclick="sensitiveWords.importExcel()" value="批量新增" />
								<input type="button" style="margin-left: 10px;"  class="btn btn-primary" onclick="sensitiveWords.batchDelete()" value="批量删除" />
							</div>



						</div>
					</div>
				</div>
				<div class="box-body">
					<table class="table table-hover table-bordered table-striped">
						<thead>
							<tr>
								<th style="width:2%;"><input type="checkbox" onclick="sensitiveWords.selectAll(event)"></th>
								<th style="width:2%;">#</th>
								<th style="width:15%;">敏感词</th>
								<th style="width:10%;">创建人</th>
								<th style="width:10%;">创建时间</th>
								<th style="width:15%;" >操作</th>
							</tr>
						</thead>
						<tbody>
							<#list page.content as u>
								<tr onclick="sensitiveWords.selectSingle(event,this)">
									<td><input type="checkbox" value="${u.vid}"></td>
									<td>${page.size * page.number+u_index+1}</td>
									<td>${u.sensitiveWord!}</td>
									<td>${u.createUserName!}</td>
									<td>${u.createTime?string("yyyy-MM-dd HH:mm:ss")}</td>
									<td><a href="javascript:void(0);" onclick="sensitiveWords.singleDelete('${u.vid}')">删除</a></td>
								</tr>
							</#list>
						</tbody>
					</table>
					<#include "../common/page.htm">
				</div>
			</div>
		</form>
	</section>
</div>
<script type="text/javascript">
    function DatePicker($beginSelector, $endSelector) {
		$beginSelector.datepicker({
			language : "zh-CN",
			autoclose : true,
			startView : 0,
			format : "yyyy-mm-dd",
			clearBtn : true,
			todayBtn : false,
			endDate : '${paramsSensitiveWords.endCreateTimeStr!}'
		}).on('changeDate', function(ev) {
			if (ev.date) {
				$endSelector.datepicker('setStartDate', new Date(ev.date.valueOf()));
			} else {
				$endSelector.datepicker('setStartDate', null);
			}
		});
		
		$endSelector.datepicker({
			language : "zh-CN",
			autoclose : true,
			startView : 0,
			format : "yyyy-mm-dd",
			clearBtn : true,
			todayBtn : false,
			startDate : '${paramsSensitiveWords.beginCreateTimeStr!}'
		}).on('changeDate', function(ev) {
			if (ev.date) {
				$beginSelector.datepicker('setEndDate', new Date(ev.date.valueOf()));
			} else {
				$beginSelector.datepicker('setEndDate', new Date());
			}
		});
	}
	// 仅选择日期
	DatePicker($("#starTime"), $("#endTime"));
    var is_submit=false;
    var ids=[]; //存放被训中敏感词的id数组
    var child_checkbox=$("table tbody input[type='checkbox']");
	var sensitiveWords = {
		query: function () {
			$(":input[name='pageNumber']").val(0);
			$("form").submit();
		},
        form : function(id){
            if(is_submit){
                return;
            }
            is_submit=true;
            id = id?id:"";
            var title = id?"修改敏感词":"新增敏感词";
            var j = 0;
            base.getView({
                url:"${ctx}/sensitivewords/form",
                success:function(result){
                    bootbox.dialog({
                        title:title,
                        message:result,
                        size:"large",
                        closeButton : false,
                        buttons:{
                            cancel:{
                                label:"取消",
                                className:"btn btn-secondary",
                                callback:function(){
                                    is_submit=false;
                                }
                            },
                            sure:{
                                label:"确定",
                                className:"btn btn-primary",
                                callback:function(){
                                    if (j != 0) {
                                        is_submit=false;
                                        return ;
                                    }
                                    j++;
                                    base.validAndAjaxSubmit($("#addSensitivewords"), $("#listForm"), function () {
                                        j = 0;
                                    });
                                    return false;
                                }
                            }
                        }
                    })
                }
            })
        },
        importExcel : function(){
            if(is_submit){
                return;
            }
            is_submit=true;
            var title = "导入Excel表格";
            var j = 0;
            base.getView({
                url:"${ctx}/sensitivewords/formExcel",
                success:function(result){
                    bootbox.dialog({
                        title:title,
                        message:result,
                        size:"large",
                        closeButton : false,
                        buttons:{
                            cancel:{
                                label:"取消",
                                className:"btn btn-secondary",
                                callback:function(){
                                    is_submit=false;
                                }
                            },
                            sure:{
                                label:"确定",
                                className:"btn btn-primary",
                                callback:function(){
                                    if (j != 0) {
                                        is_submit=false;
                                        return ;
                                    }
                                    j++;
                                    base.validAndAjaxSubmit($("#importsensitivewords_excel"), $("#listForm"), function () {
                                        j = 0;
                                    });
                                    return false;
                                }
                            }
                        }
                    })
                }
            })
        },
		selectAll:function(event){
			var obj=event.target;
			if(obj.checked){
                $("table tbody input[type='checkbox']").prop("checked",true);
                for (var i=0;i<child_checkbox.length;i++){
                    var id=child_checkbox[i].value;
                    if(ids.indexOf(id)==-1){
                        ids.push(id);
					}
				}
                console.log( ids)
            }else{
                $("table tbody input[type='checkbox']").prop("checked",false);
                ids=[];
                console.log( ids)
			}
		},
		selectSingle:function(event,tr){
            var tagName = event.target.tagName;
            obj = $($(tr).children()[0]).children()[0];
            var val = $(obj).attr("value");
            var index=ids.indexOf(val);
            //因为选中td和input的情况不一样，所以需要分别做下处理
            if(tagName=="TD"){
                if($(obj).prop('checked')) {
                    $(obj).prop('checked',false);
                    ids.splice(index,1);
                }else{
                    $(obj).prop('checked',true);
                    if(ids.indexOf(val)==-1){
                        ids.push(val);
                    }
                }
			}else if (tagName=="INPUT"){
                if(!$(obj).prop('checked')) {
                    ids.splice(index,1);
                }else{
                    if(ids.indexOf(val)==-1){
                        ids.push(val);
                    }
                }
			}
			//判断复选框是否选中，如果全部选中就选中总复选框。
            if(child_checkbox.length==ids.length){
                $("table thead input[type='checkbox']").prop("checked",true);
            }else{
                $("table thead input[type='checkbox']").prop("checked",false);
			}
            console.log( ids)
		},
		batchDelete:function(){
		    if(!ids.length){
                bootbox.alert("请选择删除的内容!");
                return;
			}
            bootbox.confirm({
                message: "你确定要删除么？",
                buttons: {
                    confirm: {
                        label: '确定'
                    },
                    cancel: {
                        label: '取消'
                    }
                },
                callback: function (result) {
                    if (result){
                        $.ajax({
                            type: 'POST',
                            url:"${ctx}/sensitivewords/batchDelete",
                            data: {
                                "ids":ids.toString()
                            },
                            success:function(res){
                                if(res.result=="success"){
                                    sensitiveWords.query();
                                }
                            }
                        })
                    }
                }
            });

		},
        singleDelete:function(id){
            bootbox.confirm({
                message: "你确定要删除么？",
                buttons: {
                    confirm: {
                        label: '确定'
                    },
                    cancel: {
                        label: '取消'
                    }
                },
                callback: function (result) {
                    if (result){
                        window.location.href="${ctx}/sensitivewords/deleteById?vid="+id;
                    }
                }
			});
        }
	}
</script>
<#include "../common/footer.htm">